/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package eventxmlparser;

import eventxmlparser.DayTime.DayOfWeek;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

/**
 *
 * @author krist_000
 */
@Root(name = "timetable")
public class TimeTable {
    
    @Attribute(name = "id")
    private String id;  
    
    @Element(name = "from")
    private Date from;
    
    @Element(name = "to")
    private Date to;    
    
    @ElementList(name = "days")
    private ArrayList<DayTime> days; 

     public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    public Date getFrom() {
        return from;
    }

    public void setFrom(Date from) {
        this.from = from;
    }

    public Date getTo() {
        return to;
    }

    public void setTo(Date to) {
        this.to = to;
    }


    public ArrayList<DayTime> getDays() {
        return days;
    }

    public void setDays(ArrayList<DayTime> days) {
        this.days = days;
    }
    
    public boolean evaluateTime(Date now) {
        
        if(now.before(from))
            return false;
        if(now.after(to))
            return false;
        
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        int iDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        DayOfWeek dof = toEnum(iDayOfWeek);
        
        for(DayTime dt :  this.getDays()) {
            if(dt.getDay() == dof) {
                
                Calendar startDate = Calendar.getInstance();
                c.setTime(now);
                Calendar endDate = Calendar.getInstance();
                c.setTime(now);
                
                
                startDate.set(Calendar.HOUR, dt.GetStartTime().getHour());
                startDate.set(Calendar.MINUTE, dt.GetStartTime().getMinute());
                endDate.set(Calendar.HOUR, dt.getEndTime().getHour());
                endDate.set(Calendar.MINUTE, dt.getEndTime().getMinute());

                if(c.after(startDate) && c.before(endDate)) {
                    return true;
                } else {
                    return false;
                }
                
            }
        }
        
        return false;        
    }

    public DayOfWeek toEnum(int dayOfWeek) {
            switch(dayOfWeek) {
                case Calendar.MONDAY:
                    return DayOfWeek.monday;
                case Calendar.TUESDAY:
                    return DayOfWeek.tuesday;
                case Calendar.WEDNESDAY:
                    return DayOfWeek.wednesday;
                case Calendar.THURSDAY:
                    return DayOfWeek.thursday;
                case Calendar.FRIDAY:
                    return DayOfWeek.friday;
                case Calendar.SATURDAY:
                    return DayOfWeek.saturday;
                case Calendar.SUNDAY:
                    return DayOfWeek.sunday;
                default:
                    return DayOfWeek.monday;
        }
    }
    
    @Override
    public String toString() {
        String str = "TimeTable: " + getId() + " from " + getFrom() + " to " + getTo() + "\n";
        for(DayTime day : getDays())
        {
            str += day.toString();
        }
        str += "\n";
        return str;
    }
    
    
}
